package com.wxw.BaseLibrary.tool;

import android.app.Activity;
import android.app.Application;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by david on 14-3-5.
 */
public class ExitApplication extends Application {
    private List<Activity> activityList = new LinkedList<Activity>();

    private static ExitApplication exitApplication=null;
    private ExitApplication(){}
    private ApplicationExitingListener applicationExitingListener;


    public static ExitApplication getInstance(){
        if (exitApplication == null) {
            exitApplication = new ExitApplication();
        }
        return exitApplication;
    }

    public void addActivity(Activity activity){
        if (!activityList.contains(activity)) {
            activityList.add(activity);
        }
    }

    public void exit(){
//        RequestManager.getRequestManager().setRunning(false);
//        SystemStatus.notificationManager.cancelAll();
        for (Activity activity : activityList) {
            activity.finish();
        }
//        if (DBTool.getInstance() != null)
//            DBTool.getInstance().close();
        if (applicationExitingListener!=null) {
            applicationExitingListener.onExiting(this, "Application exited!");
        }
        System.exit(0);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

    public void setApplicationExitingListener(ApplicationExitingListener applicationExitingListener) {
        this.applicationExitingListener = applicationExitingListener;
    }

    public interface ApplicationExitingListener {
        void onExiting(Object sender, String event);
    }
}
